

export async function loadCsvData(nodes_csv, edges_csv) {
  const rawNodes = await loadCSV(nodes_csv);
  const rawEdges = await loadCSV(edges_csv);

  const nodes = rawNodes.map(n => ({
    id: n.id,
    lon: parseFloat(n.lon),
    lat: parseFloat(n.lat),
  }));

  const edges = rawEdges.map(e => [e.from, e.to]);
  return { nodes, edges };
}

function loadCSV(url) {
    return fetch(url)
      .then(response => response.text())
      .then(csvText => {
        const rows = csvText.replace(/\r/g, '').split('\n').filter(row => row.trim() !== '');
        const headers = rows[0].split(',');
        return rows.slice(1).map(row => {
          const values = row.split(',');
          return headers.reduce((obj, header, index) => {
            obj[header] = values[index]?.trim();
            return obj;
          }, {});
        });
      })
      .catch(error => {
        console.error(`加载 ${url} 失败:`, error);
        return []; 
      });;
  }